package o;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.PermissionChecker;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.util.Calendar;

public class al {
    private static al ˏ;
    private final Context ˊ;
    private final LocationManager ˋ;
    private final e ˎ = new e();

    static class e {
        long ʻ;
        boolean ˊ;
        long ˋ;
        long ˎ;
        long ˏ;
        long ॱ;

        e() {
        }
    }

    public static al ॱ(@NonNull Context context) {
        if (ˏ == null) {
            Context applicationContext = context.getApplicationContext();
            ˏ = new al(applicationContext, (LocationManager) applicationContext.getSystemService("location"));
        }
        return ˏ;
    }

    @VisibleForTesting
    al(@NonNull Context context, @NonNull LocationManager locationManager) {
        this.ˊ = context;
        this.ˋ = locationManager;
    }

    public boolean ॱ() {
        e eVar = this.ˎ;
        if (ˊ()) {
            return eVar.ˊ;
        }
        Location ˎ = ˎ();
        if (ˎ != null) {
            ˎ(ˎ);
            return eVar.ˊ;
        }
        Log.i("TwilightManager", "Could not get last known location. This is probably because the app does not have any location permissions. Falling back to hardcoded sunrise/sunset values.");
        int i = Calendar.getInstance().get(11);
        return i < 6 || i >= 22;
    }

    @SuppressLint({"MissingPermission"})
    private Location ˎ() {
        Location ˎ;
        Location location = null;
        if (PermissionChecker.ˊ(this.ˊ, "android.permission.ACCESS_COARSE_LOCATION") == 0) {
            ˎ = ˎ("network");
        } else {
            ˎ = null;
        }
        if (PermissionChecker.ˊ(this.ˊ, "android.permission.ACCESS_FINE_LOCATION") == 0) {
            location = ˎ("gps");
        }
        if (location == null || ˎ == null) {
            if (location == null) {
                location = ˎ;
            }
            return location;
        } else if (location.getTime() > ˎ.getTime()) {
            return location;
        } else {
            return ˎ;
        }
    }

    @RequiresPermission(anyOf = {"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"})
    private Location ˎ(String str) {
        try {
            if (this.ˋ.isProviderEnabled(str)) {
                return this.ˋ.getLastKnownLocation(str);
            }
        } catch (Throwable e) {
            Log.d("TwilightManager", "Failed to get last known location", e);
        }
        return null;
    }

    private boolean ˊ() {
        return this.ˎ.ʻ > System.currentTimeMillis();
    }

    private void ˎ(@NonNull Location location) {
        long j;
        e eVar = this.ˎ;
        long currentTimeMillis = System.currentTimeMillis();
        an ॱ = an.ॱ();
        ॱ.ˊ(currentTimeMillis - HwAccountConstants.CHECK_SITE_COUNTRY_DURATION, location.getLatitude(), location.getLongitude());
        long j2 = ॱ.ˊ;
        ॱ.ˊ(currentTimeMillis, location.getLatitude(), location.getLongitude());
        boolean z = ॱ.ˎ == 1;
        long j3 = ॱ.ॱ;
        long j4 = ॱ.ˊ;
        ॱ.ˊ(HwAccountConstants.CHECK_SITE_COUNTRY_DURATION + currentTimeMillis, location.getLatitude(), location.getLongitude());
        long j5 = ॱ.ॱ;
        if (j3 == -1 || j4 == -1) {
            j = 43200000 + currentTimeMillis;
        } else {
            if (currentTimeMillis > j4) {
                j = 0 + j5;
            } else if (currentTimeMillis > j3) {
                j = 0 + j4;
            } else {
                j = 0 + j3;
            }
            j += 60000;
        }
        eVar.ˊ = z;
        eVar.ˏ = j2;
        eVar.ॱ = j3;
        eVar.ˎ = j4;
        eVar.ˋ = j5;
        eVar.ʻ = j;
    }
}
